package dyyx.jetty;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;

public class HelloServletMain {
	
	public static void main(String[] args) throws Exception {
		int port = 8080;
		Server server = createServer(port);
		server.start();
		server.join();
	}

	public static Server createServer(int port) {
		Server server = new Server(port);
		// 引入 jetty-servlet
		ServletHandler handler = new ServletHandler();
		server.setHandler(handler);
		handler.addServletWithMapping(HelloServlet.class, "/*");
		return server;
	}
	
	@SuppressWarnings("serial")
	public static class HelloServlet extends HttpServlet {
		@Override
		protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
			response.setStatus(HttpServletResponse.SC_OK);
			response.setContentType("text/html");
			response.setCharacterEncoding("utf-8");
			response.getWriter().println("Hello Servlet,"+new Date());
		}
	}
}